package cn.etg.day06;

import java.io.*;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

/**
 * noteText06.txt
 * 要求用户输入一个文件，该文件应当是Test07程序生成的文件，然后将该文件中所有字符读取
 * 出来，并以UTF-8编码写入到另一个文件中，实现文件转码工作，该文件取名格式:原文件名_utf.txt。
 * @author Xiloer
 *
 */
public class Test07 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String fileName = sc.next();
        String[] fileNames = fileName.split("\\.");
        FileInputStream fis = null;
        FileOutputStream fos = null;
        InputStreamReader isr = null;
        OutputStreamWriter osw = null;
        BufferedReader br = null;
        PrintWriter pw = null;
        try {
            fis = new FileInputStream("./src/api_homework/day06/" + fileName);
            fos = new FileOutputStream("./src/api_homework/day06/" + fileNames[0] + "_utf." + fileNames[1]);
            isr = new InputStreamReader(fis, Charset.forName("GBK"));
            osw = new OutputStreamWriter(fos, StandardCharsets.UTF_8);
            br = new BufferedReader(isr);
            pw = new PrintWriter(osw);
            String str;
            while ((str = br.readLine()) != null) {
                pw.println(str);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                br.close();
                pw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
